<?php
include_once('../lib/config.php');
require ($war_root.'/lib/verify.php');
require ($war_root.'/lib/has_char.php');
require ($war_root.'/lib/is_approved.php');
require ($war_root.'/lib/is_admin.php');

$_category = "Admin";
$_title = "";
$admin_active = 1;
$log_char = $character["character_name"];

require ($war_root.'/lib/head.php');

$page = @$_GET ['p'];
$system_id = @$system ["system_id"];
boxTop ( "$_category: " );

if ($page == 'view') {
  $character = $_POST ["character"];
  $sql = "select i.item_id, w.loot_level, i.item_name, w.loot_id
          from wm_loots w, item_cache i
          where w.loot_character = '$character' and
                w.loot_item = i.item_id
          order by i.item_name";
  $result = $db->query ($sql);

  echo "Loot:<br/>";
  echo '<form action="'.$war_http.'/admin/bids.php?p=update" method="post"><p>';
  echo '<input type="hidden" name="character" value="'.$character.'">';
  while ($item = $db->fetch_assoc($result)) {
    echo show_pure($item).' currently registered as <b>'.bid2text($item['loot_level']).'</b>.<br/>';
    
    echo '<select name="loot-'.$item['loot_id'].'">';
    for ($i=bid_max(); $i > 0; $i--) {
      echo '<option value="'.$i.'">'.bid2text($i).'</option>';
    }
    echo '<option value="0">Delete bid</option>';
    echo '<option selected value="-1">No change</option>';
    echo '</select><br/><br/>';
  }
  echo '<input type="submit" value="Update bids">';
  echo '</form>';

} elseif ($page == 'update') {
  $character = $_POST ["character"];

  foreach(array_keys(@$_POST) as $key) {
    $tmp = substr($key, 0, 5);
    if ($tmp == 'loot-') {
      $loot_id = substr($key, 5);
      $bid = $_POST[$key];
      # Fetch item_id for showing result
      $sql ="select loot_item as item_id from wm_loots where loot_id = $loot_id";
      $r = $db->query($sql);
      $i = $db->fetch_assoc($r);
      $item = $i['item_id'];
      # Udpate bid or delete it
      if ($bid > 0) {
        $sql = "UPDATE wm_loots SET loot_level = $bid WHERE loot_id = $loot_id";
        $res = $db->query($sql);
        if ($res != 1) {
          die('Problems updating item: "'.$item.'" with bid: "'.bid2text($bid).'", please contact an administrator.');
        } else {
          $wish['item_id'] = $item;
          echo show_pure($wish).' - Successfully changed bid to: '.bid2text($bid).'.<br/>';
        }
      } elseif ($bid == 0) {
        $sql = "DELETE FROM wm_loots WHERE loot_item = $item AND loot_character = $character";
        $res = $db->query($sql);
        if ($res != 1) {
          die('Problems deleting item: "'.$item.'", please contact an administrator.');
        } else {
          $wish['item_id'] = $item;
          echo show_pure($wish).' - Successfully deleted.<br/>';
        }
      }
    }
  }
  reprocessDKP($db, $system_id);
  echo "<br/>Reprocessed DKP.";

} else {
  $sql = "select character_id, character_name 
          from wm_characters 
          where character_system_id = $system_id
          order by character_name";
  $result = $db->query ($sql);

  echo "Select character you want to review bids for.<br/>";
  echo '<form action="'.$war_http.'/admin/bids.php?p=view" method="post"><p>';
  echo '<select name="character">';
  while ($char = $db->fetch_assoc($result)) {
    echo '<option value="'.$char["character_id"].'">'.$char["character_name"].'</option>';
  }
  echo '</select><br/>';
  echo '<input type="submit" value="Show loot">';
  echo '</form>';
  
} 

boxBot ();

?>